﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
//using System.Runtime.InteropServices;

namespace quanlynhasach
{
    public partial class frmBanhang : Form
    {
        Connection db = new Connection();
        ChiTietBanHang CTHD = new ChiTietBanHang();
        quanlynhasach.Linksql.BanHangsql bhsql = new Linksql.BanHangsql();
        public frmBanhang()
        {
            InitializeComponent();
            rdTatCa.Checked = true;
        }

        private ChiTietBanHang GetData()
        {
            ChiTietBanHang CTBH = new ChiTietBanHang();
            CTBH.SoCTHD = textBoxSoCTHD.Text;
            CTBH.MaHD = textBoxSoHD.Text;
            CTBH.MaSachCTHD = textBoxMaHang.Text;
            CTBH.SoLuongCTHD = int.Parse(textBoxSoLuong.Text);
            CTBH.DonGiaCTHD = int.Parse(textBoxDonGia.Text);
            CTBH.ThanhTienCTHD = textBoxThanhTien.Text;
            CTBH.DonViCTHD = textboxdonvi.Text;
            CTBH.Thue = int.Parse(textBoxVat.Text);
            return CTBH;
        }

        private HoaDonBan GetDataHD()
        {
            HoaDonBan HDB = new HoaDonBan();
            HDB.MaHD = textBoxSoHD.Text;
            HDB.MaKHHD = textBoxTenKhachHang.Text;
            HDB.MaNVHD = textBoxtenNhanVien.Text;
            textBoxThanhToan.Text = "0";
            HDB.TongTienHD = 0;
            return HDB;
        }

        public void Enable(bool a)
        {
            btnThemCTHD.Enabled = a;
            btnXoaCTHD.Enabled = a;
            btnHuyCTHD.Enabled = a;
            btnSuaCTHD.Enabled = a;
            btnTongTien.Enabled = a;
        }


        private void Sum()
        {
            if (textBoxSoLuong.Text == "")
            {
                textBoxThanhTien.Text = "0";
                return;
            }

            CTHD.SoLuongCTHD = int.Parse(textBoxSoLuong.Text);
            CTHD.DonGiaCTHD = int.Parse(textBoxDonGia.Text);
            int ThanhTien = CTHD.SoLuongCTHD * CTHD.DonGiaCTHD;
            textBoxThanhTien.Text = ThanhTien.ToString();
        }

        

        private void frmBanhang_Load(object sender, EventArgs e)
        {
            btnTaoHoaDon.Enabled = false;
            btnTongTien.Enabled = false;
            textBoxSoCTHD.Text = bhsql.SearchDaTaGridSCTHD();
            textBoxSoHD.Text = bhsql.SearchDaTaGridSHD();
            dtgdBanHang.DataSource = bhsql.getCTHD();
            this.Dock = DockStyle.Fill;
            cmbTenSach.ResetText();
            cmbKhachHang.ResetText();
            cmbKhachHang.DropDownStyle = ComboBoxStyle.DropDownList;
            textBoxMaNV.Text = frmMain.user.ToUpper();
            cmbTenSach.DropDownStyle = ComboBoxStyle.DropDownList;
            DataTable dt = db.getDS("Select TenSach From Sach");
            int Count;
            Count = dt.Rows.Count;
            for (int i = 0; i < Count; i++)
            {
                string stringcomboBox = dt.Rows[i]["TenSach"].ToString();
                if (stringcomboBox != "N/A")
                    cmbTenSach.Items.Add(stringcomboBox);
            }
            dt.Clear();
            dt = db.getDS("Select MaKH From KhachHang");
            Count = dt.Rows.Count;
            for (int i = 0; i < Count; i++)
            {
                string stringcomboBox = dt.Rows[i]["MaKH"].ToString();
                cmbKhachHang.Items.Add(stringcomboBox);
            }
            
            
            
        }

        private void btnThemCTHD_Click(object sender, EventArgs e)
        {
            textBoxSoCTHD.Text = bhsql.SearchDaTaGridSCTHD();
            ChiTietBanHang bh = GetData();
            if (int.Parse(textBoxSoLuong.Text) > 0)
            {
                try
                {
                    if (bhsql.ThemCTHD(bh))
                    {
                        MessageBox.Show("Thêm Thành Công");
                        btnTongTien.Enabled = true;
                        dtgdBanHang.DataSource = bhsql.getCTHD(textBoxSoHD.Text);
                        int a = 0;
                        for(int i=0; i<dtgdBanHang.RowCount; i++)
                            a += int.Parse(dtgdBanHang.Rows[i].Cells["ThanhTien"].Value.ToString());
                        textBoxTongCong.Text = a.ToString();
                    }
                }
                catch
                {
                    MessageBox.Show("");
                }
                
                
            }
            else
                MessageBox.Show("Số Lượng Phải Lớn Hơn 0");
        }

        private void btnSuaCTHD_Click(object sender, EventArgs e)
        {
            ChiTietBanHang BH = GetData();
            if (int.Parse(textBoxSoLuong.Text) > 0)
            {
                try
                {
                    if (bhsql.SuaCTHD(BH))
                    {
                        MessageBox.Show("Sửa Thành Công");
                        dtgdBanHang.DataSource = bhsql.getCTHD(textBoxSoHD.Text);
                        int a = 0;
                        for (int i = 0; i < dtgdBanHang.RowCount; i++)
                            a += int.Parse(dtgdBanHang.Rows[i].Cells["ThanhTien"].Value.ToString());
                        textBoxTongCong.Text = a.ToString();
                    }
                }
                catch
                {
                    MessageBox.Show("");
                }


            }
            else
                MessageBox.Show("Số Lượng Phải Lớn Hơn 0");
        }

        private void btnXoaCTHD_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Bạn Có Chắc Muốn Xóa mặt hàng " + cmbTenSach.Text + " Không ?", "Xóa", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
            {
                if (bhsql.XoaCTHD(textBoxSoCTHD.Text))
                {
                    MessageBox.Show("Xóa Thành Công!");
                }
                else
                    MessageBox.Show("Không Thể Xóa");
            }
            dtgdBanHang.DataSource = bhsql.getCTHD(textBoxSoHD.Text);
            int a = 0;
            for (int i = 0; i < dtgdBanHang.RowCount; i++)
                a += int.Parse(dtgdBanHang.Rows[i].Cells["ThanhTien"].Value.ToString());
            textBoxTongCong.Text = a.ToString();
        }

        private void btnHuyCTHD_Click(object sender, EventArgs e)
        {
            int i = int.Parse(textBoxSoCTHD.Text.Substring(4)) - 1;
            while (true)
            {
                
                if (bhsql.check("CTHD"+i.ToString(),textBoxSoHD.Text))
                {
                    bhsql.XoaCTHD("CTHD" + i.ToString());
                }
                else
                    break;
                i--;
               
            }
            try
            {
                if (bhsql.XoaHD(textBoxSoHD.Text))
                {
                    MessageBox.Show("Xoá Thành Công");
                }
            }
            catch

            { }
            dtgdBanHang.DataSource = bhsql.getCTHD();
            rdTatCa.Enabled = true;
            rdTatCa.Checked = true;
        }

        private void btnTongTien_Click(object sender, EventArgs e)
        {
                HoaDon hd = new HoaDon();
                hd.source(1, textBoxSoHD.Text);
                hd.ShowDialog();
                btnTaoHoaDon.Enabled = true;
                btnThemCTHD.Enabled = false;
                textBoxSoHD.Text = bhsql.SearchDaTaGridSHD();
                textBoxSoCTHD.Text = bhsql.SearchDaTaGridSCTHD();
                dtgdBanHang.DataSource = bhsql.getCTHD(textBoxSoHD.Text);
                rdTatCa.Enabled = true;
                rdTatCa.Checked = true;
        }

        private void btnThoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void cmbTenSach_SelectedIndexChanged(object sender, EventArgs e)
        {
            
            DataTable dt = new DataTable();
            dt = db.getDS("Select * From Sach Where TenSach = '" + cmbTenSach.Text + "'");
            textBoxMaHang.Text = dt.Rows[0]["MaSach"].ToString();
            textBoxSoLuong.Text = dt.Rows[0]["SoLuong"].ToString();
            textBoxDonGia.Text = dt.Rows[0]["GiaBan"].ToString();
            textboxdonvi.Text = dt.Rows[0]["DonVi"].ToString();
            textBoxVat.Text = dt.Rows[0]["Thue"].ToString();
            Sum();
        }

        private void textboxSoLuong_TextChanged(object sender, EventArgs e)
        {
            int i;
            if (!int.TryParse(textBoxSoLuong.Text, out i))
            {
                textBoxThanhTien.Text = "0";
                if (textBoxSoLuong.Text != "")
                    MessageBox.Show("Số Lượng Phải Là Số Nguyên");
                return;
            }
            Sum();
        }

        private void rdTatCa_CheckedChanged(object sender, EventArgs e)
        {
            textBoxSoLuong.Enabled = false;
            cmbTenSach.Enabled = false;
            btnTaoHoaDon.Enabled = false;
            checkBoxKhachHang.Checked = false;
            checkBoxKhachHang.Enabled = false;
            cmbKhachHang.Enabled = false;
            dtgdBanHang.DataSource = bhsql.getCTHD();
            Enable(false);
        }

        private void rdHienTai_CheckedChanged(object sender, EventArgs e)
        {
            textBoxTongCong.Text = "0";
            textBoxPhaiTra.Text = "0";
            textBoxSoLuong.Enabled = true;
            cmbTenSach.Enabled = true;
            checkBoxKhachHang.Enabled = true;
            cmbKhachHang.Enabled = true;
            checkBoxKhachHang.Checked = true;
            textBoxMaNV.Text = frmMain.user.ToUpper();
            btnTaoHoaDon.Enabled = true;
            textBoxSoCTHD.Text = bhsql.SearchDaTaGridSCTHD();
            textBoxSoHD.Text = bhsql.SearchDaTaGridSHD();
            dtgdBanHang.DataSource = bhsql.getCTHD(textBoxSoHD.Text);
            Enable(true);
            btnTongTien.Enabled = false;
            btnThemCTHD.Enabled = false;
            if (!btnTaoHoaDon.Enabled)
                btnThemCTHD.Enabled = true ;
        }

        private void checkBoxKhachHang_CheckStateChanged(object sender, EventArgs e)
        {
            if (checkBoxKhachHang.Checked)
            {
                cmbKhachHang.Text = "N/A";
            }
            else
            {
                cmbKhachHang.Text = "";
            }
        }

       

        private void dtgdBanHang_RowEnter(object sender, DataGridViewCellEventArgs e)
        {
            int Rs = e.RowIndex;
            textBoxSoHD.Text = dtgdBanHang.Rows[Rs].Cells["MaHoaDon"].Value.ToString();
            textBoxSoCTHD.Text = dtgdBanHang.Rows[Rs].Cells["SoHoaDon"].Value.ToString();
            textBoxMaNV.Text = bhsql.GetMaNV(dtgdBanHang.Rows[Rs].Cells["MaNhanVien"].Value.ToString());
            textBoxtenNhanVien.Text = dtgdBanHang.Rows[Rs].Cells["MaNhanVien"].Value.ToString();
            textBoxTenKhachHang.Text = dtgdBanHang.Rows[Rs].Cells["MaKH"].Value.ToString();
            cmbKhachHang.Text = bhsql.GetMaKH(dtgdBanHang.Rows[Rs].Cells["MaKH"].Value.ToString());
            textBoxMaHang.Text = dtgdBanHang.Rows[Rs].Cells["MaSach"].Value.ToString();
            DataTable dt = new DataTable();
            dt = db.getDS("Select * From Sach Where MaSach = '" + textBoxMaHang.Text + "'");
            cmbTenSach.Text = dt.Rows[0]["TenSach"].ToString();
            textBoxSoLuong.Text = dtgdBanHang.Rows[Rs].Cells["SoLuong"].Value.ToString();
            textBoxDonGia.Text = dtgdBanHang.Rows[Rs].Cells["DonGia"].Value.ToString();
            textBoxThanhTien.Text = dtgdBanHang.Rows[Rs].Cells["ThanhTien"].Value.ToString();
            textBoxVat.Text = dtgdBanHang.Rows[Rs].Cells["Thue"].Value.ToString();
            if (rdTatCa.Checked == true)
            {
                dt = bhsql.getCTHD(textBoxSoHD.Text);
                int a = 0;
                for (int i = 0; i < dt.Rows.Count; i++)
                    a += int.Parse(dt.Rows[i]["ThanhTien"].ToString());
                textBoxTongCong.Text = a.ToString();
                dt = db.getDS("Select * From HoaDon Where MaHoaDon = '" + textBoxSoHD.Text + "'");
                textBoxPhaiTra.Text = dt.Rows[0]["TongTien"].ToString();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            textBoxThanhToan.Text = "0";
            textBoxTongCong.Text = "0";
            textBoxThoiLai.Text = "0";
            textBoxPhaiTra.Text = "0";
            btnThemCTHD.Enabled = true;
            rdTatCa.Enabled = false;
            HoaDonBan hdb = new HoaDonBan();
            hdb = GetDataHD();
            bhsql.ThemHD(hdb);
            btnTaoHoaDon.Enabled = false;
        }

        private void cmbKhachHang_SelectedIndexChanged(object sender, EventArgs e)
        {
            textBoxTenKhachHang.Text = bhsql.GetNameKH(cmbKhachHang);
        }

        private void TextboxNhanVien(object sender, EventArgs e)
        {
            textBoxtenNhanVien.Text = bhsql.GetNameNV(textBoxMaNV.Text);
        }

        private void textBoxTongCong_TextChanged(object sender, EventArgs e)
        {
            float a = 0;
            for (int i = 0; i < dtgdBanHang.RowCount; i++)
            {
                float b = float.Parse(dtgdBanHang.Rows[i].Cells["ThanhTien"].Value.ToString());
                int c= int.Parse(dtgdBanHang.Rows[i].Cells["Thue"].Value.ToString());
                a += b + b * c / 100;
            }
            textBoxPhaiTra.Text = a.ToString();
        }

        private void textBoxThanhToan_TextChanged(object sender, EventArgs e)
        {
            float a,b;
            if (!float.TryParse(textBoxThanhToan.Text, out b))
            {
                if(textBoxThanhToan.Text != "")
                    MessageBox.Show("Số Tiền Thanh Toán Phải Là Số");
                return;
            }
            a = float.Parse(textBoxThanhToan.Text) - float.Parse(textBoxPhaiTra.Text);
            textBoxThoiLai.Text = a.ToString();
        }

        private void checkBoxKhachHang_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBoxKhachHang.Checked)
            {
                cmbKhachHang.Text = "N/A";
                cmbKhachHang.Enabled = false;
            }
            else
                cmbKhachHang.Enabled = true;
        }

    }
}
